home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / java / applets / graphs / linedi~1.jav < prev    next >
Encoding:
Text File  |  1995-10-31  |  8.9 KB  |  310 lines

  1. /* 
  2.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. import java.lang.*; 
  31. import java.awt.*;
  32. import java.net.*;
  33. import java.util.*;
  34.  
  35. /**
  36.  * contains lines and draws these lines in the given display
  37.  *
  38.  * @author      Siebe R. Brouwer
  39.  * version      1.1, 13 october 1995
  40.  */
  41.  
  42. class LineDiagram extends Diagram {
  43.     
  44.     protected Display display;
  45.     protected int virtualYgap;
  46.     protected int virtualXgap;
  47.     protected int virtualXbegin;
  48.     protected int virtualYbegin;
  49.     protected int xBegin;
  50.     protected int yBegin;
  51.     protected Vector lines;
  52.         
  53.     // constructor of this class
  54.     LineDiagram(int vXbegin, int vYbegin, int vXgap, int vYgap, Display d) {
  55.         display = d;
  56.     float i = (float)vXbegin / (float)vXgap;
  57.     xBegin = (int)(i * display.xGap());
  58.     i = (float)vYbegin / (float)vYgap;
  59.     yBegin = (int)(i * display.yGap());
  60.         virtualXbegin = vXbegin;
  61.     virtualYbegin = vYbegin;
  62.         virtualXgap = vXgap;
  63.     virtualYgap = vYgap;
  64.     lines = new Vector();
  65.     }
  66.     
  67.     // create a new line for in the diagram
  68.     public void createLine(Color color, String label) {
  69.     if(!hasLine(label)) {                  
  70.             lines.addElement(new Line(color, label));    
  71.     }
  72.     }  
  73.     
  74.     // add dot to current line
  75.     public void addDot(int x, int y, String label, String lineLabel) {
  76.         if(hasLine(lineLabel)) {
  77.         int index = getLine(lineLabel);
  78.         Line line = (Line)lines.elementAt(index);
  79.         int xValue = convertXdot(x);
  80.         int yValue = convertYdot(y);
  81.         line.addDot(xValue, yValue, label);
  82.     
  83.     }
  84.     }
  85.     
  86.     // draw the lines who are in the diagram
  87.     public void drawData(Graphics g, int gaps, int left, int moved) { 
  88.     int xStart = display.xStart();
  89.     int yStart = display.yStart();
  90.     int xTop = display.xTop();
  91.     int yTop = display.yTop();
  92.     int xGap = display.xGap();
  93.     int yGap = display.yGap();
  94.     
  95.     int vGap=0;
  96.     int vXrest=0;
  97.     int vXgap=0;
  98.     int vXbegin=0;
  99.     int firstGap=0;
  100.     int vFirstGap=0;
  101.     int vYgap = virtualYbegin;
  102.     
  103.     if(gaps == 0 && left == 0) { 
  104.         firstGap = xGap;
  105.         vXbegin = virtualXbegin;
  106.         vFirstGap= virtualXbegin + virtualXgap;
  107.     }
  108.     else if(gaps < 0 || left < 0) {
  109.         float value = ((left == 0)?0:(float)left /(float)xGap);
  110.         vGap = (int)(value * (float)virtualXgap);
  111.         vXgap = gaps * virtualXgap;
  112.         vXbegin = virtualXbegin - (vGap + vXgap);
  113.         firstGap = xGap + left;
  114.         vFirstGap = vXbegin + (virtualXgap + vGap);
  115.     }
  116.     
  117.     else if(gaps > 0 || left > 0) {
  118.         float value = ((left == 0)?0 : (float)left / (float)xGap);
  119.         vGap = (int)(value * (float)virtualXgap);
  120.         vXgap = gaps * virtualXgap;
  121.         vXbegin = virtualXbegin - (vGap + vXgap);
  122.         firstGap = left;
  123.         vFirstGap = vXbegin+ vGap;
  124.     }   
  125.     if(left == 0) {
  126.         g.drawString(Integer.toString(vXbegin), xStart, yStart + 15);
  127.     }
  128.     for(int i = firstGap +xStart; i <= xTop; i += xGap) {
  129.         g.drawString(Integer.toString(vFirstGap), i, yStart + 15);
  130.         vFirstGap += virtualXgap;
  131.     }
  132.     for(int i = yStart; i >= yTop; i -= yGap) {
  133.         g.drawString(Integer.toString(vYgap), xStart - 40, i);
  134.         vYgap += virtualYgap;
  135.     }    
  136.     int xIndex = xTop - ((xTop - xStart) / 3);
  137.     int yIndex=50;
  138.     g.setColor(Color.black);
  139.     g.drawString("INDEX",xIndex, yIndex);
  140.     yIndex += 5;
  141.     g.drawLine(xIndex, yIndex, xIndex + 30, yIndex);
  142.     for(Enumeration e = lines.elements();e.hasMoreElements() ; ) {
  143.         yIndex += 15;
  144.         Line line = (Line)e.nextElement();
  145.             line.drawIndex(g, xIndex, yIndex);         
  146.         line.drawLine(g, moved, xStart, yStart, xTop, yTop);
  147.     }
  148.     }
  149.     
  150.     // give true if there is a line with the desired label
  151.     protected boolean hasLine(String label) {
  152.     for(Enumeration e = lines.elements();e.hasMoreElements() ; ) {
  153.         Line line= (Line)e.nextElement();
  154.             if (line.label() == label) {
  155.      
  156.         return true;
  157.         }
  158.     }
  159.     
  160.     return false;
  161.     }
  162.     
  163.     // get the line with the label
  164.     protected int getLine(String label) {
  165.     for(Enumeration e = lines.elements();e.hasMoreElements() ; ) {
  166.         Line line = (Line)e.nextElement();
  167.             if (line.label() == label) {
  168.            return lines.indexOf(line);
  169.         }
  170.     }
  171.         return -1;        
  172.     }
  173.     
  174.     /* convert i to an integer value, the value represents the distance
  175.      * from the xStart to the y value of the dot
  176.      */
  177.     protected int convertXdot(int i) {
  178.     int xGap = display.xGap();
  179.     float virtual = (float)i / (float)virtualXgap;
  180.     int virtual2 =(int)(virtual * xGap);
  181.     return virtual2 - xBegin;
  182.     }
  183.     
  184.     /* convert i to an integer value, the value represents the disctance    
  185.      * from yStart to the y value of the dot
  186.      */
  187.     protected int convertYdot(int i) {
  188.         int yGap = display.yGap();
  189.     float virtual = (float)i / (float)virtualYgap;
  190.     virtual = virtual * yGap;
  191.     return (int)(virtual - yBegin);
  192.     }
  193. }
  194.  
  195. // class line
  196. class Line {
  197.     
  198.     protected int type;
  199.     public Color color;
  200.     protected String label;
  201.     protected Vector dots;
  202.     
  203.     // constructor
  204.     Line(Color c, String l) {
  205.     label = l;
  206.         color = c;
  207.     dots = new Vector();
  208.     }
  209.  
  210.     // draw the dots and the lines between the dots to the graphics object
  211.     public void drawLine(Graphics g, int moved, int xS, int yS, int xT, int yT) {
  212.         Dot dot;
  213.     int xReal=0;
  214.     int yReal=0;
  215.     int prevXreal= 0;
  216.     int prevYreal = 0;
  217.     
  218.     Graphics graphics = g.create();
  219.     graphics.clipRect(xS + 1, yT, xT - xS, yS - yT);
  220.     boolean drawLine = false;
  221.     for(Enumeration e = dots.elements();e.hasMoreElements() ; ) {
  222.         dot = (Dot)e.nextElement();
  223.         xReal = (xS + dot.xValue()) + moved;
  224.         yReal = yS - dot.yValue();
  225.         if(drawLine) {
  226.         graphics.drawLine(prevXreal, prevYreal, xReal, yReal);
  227.         }
  228.         graphics.setColor(color);
  229.         graphics.drawOval(xReal, yReal, 2, 2);
  230.         graphics.fillOval(xReal, yReal, 2, 2);
  231.         graphics.drawString(dot.label(), xReal, yReal - 3);
  232.         drawLine = true;
  233.         prevXreal = xReal;
  234.         prevYreal = yReal;
  235.         }    
  236.     }
  237.     
  238.     // draw the index of this line to the graphics object
  239.     public void drawIndex(Graphics g, int x, int y) {    
  240.     g.setColor(Color.black);
  241.     g.drawRect(x,y,5,5);
  242.     g.setColor(color);
  243.     g.fillRect(x+1,y+1,4,4);
  244.     g.setColor(Color.black);
  245.     g.drawString(label, x+10,y+5);
  246.     }
  247.     
  248.     /* create a new dot object and at him to the list of dots,
  249.      * the dots in the list are stored in order of there xValue
  250.      */
  251.     public void addDot(int x, int y, String label) {
  252.     Dot dot;
  253.           Enumeration e;
  254.     for(e = dots.elements();e.hasMoreElements() ; ) {
  255.         dot = (Dot)e.nextElement();
  256.             if(x < dot.xValue()) {
  257.         int index = dots.indexOf(dot);
  258.         dots.insertElementAt(new Dot(x,y,label), index);
  259.         break;
  260.         }
  261.         else if(x == dot.xValue()) {
  262.         break;
  263.         }
  264.     }
  265.     if(! e.hasMoreElements()|| dots.isEmpty()) {
  266.         dots.addElement(new Dot(x,y,label));
  267.     }
  268.     }
  269.     
  270.     // return the label of this line
  271.     public String label() {
  272.     return label;
  273.     }
  274.     
  275.     // return the color of this line
  276.     public Color color() {
  277.         return color;
  278.     }
  279. }
  280.  
  281. // class dot
  282. class Dot {    
  283.     protected int xValue;
  284.     protected int yValue;
  285.     protected String label;
  286.  
  287.     // constructor 
  288.     Dot(int x, int y, String l) {
  289.     xValue = x;
  290.     yValue = y;
  291.     label = l;
  292.     }
  293.     
  294.     // return the xValue
  295.     public int xValue() {
  296.         return xValue;
  297.     }
  298.     
  299.     // return the yValue
  300.     public int yValue() {
  301.         return yValue;
  302.     }
  303.     
  304.     // return the label
  305.     public String label() {
  306.         return label;
  307.     }
  308. }
  309.  
  310.